<?php
namespace plugin\entity_manager\src\join;

/**
 * The MIT License
 *
 * Copyright (c) <year> <copyright holders>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @author Damiano Ciarla
 * @package plugin
 * @subpackage entity_manager/class/abstract/join/find
 */
class ManagerFind extends \plugin\entity_manager\src\join\AbstractJoinManager implements \plugin\entity_manager\src\query\InterfaceQuery
{
	public function __construct(\plugin\entity_manager\src\query\Find $functionFind)
	{
		$this->_functionInstance = $functionFind;
	}

	/**
	 * Metodo che mette in join una entity con la entity attuale verificando
	 * se la relazione tra le due tabelle è corretta o meno
	 *
	 * @param \plugin\entity_manager\src\AbstractEntity $entity Istanza della entity
	 *
	 * @return Find
	 */
	public function join($joinType, \plugin\entity_manager\src\AbstractEntity $entity, $previusEntity = null, $customJoin = null)
	{
		if(!$this->_checkPreviusEntity($entity, &$previusEntity, &$customJoin))
		{
			/**
			 * @see \plugin\entity_manager\src\exception\EntityManagerException::PREVIUS_ENTITY_NOT_FOUND
			 */
			throw new \plugin\entity_manager\src\exception\EntityManagerException($previusEntity, \plugin\entity_manager\src\exception\EntityManagerException::PREVIUS_ENTITY_NOT_FOUND);
		}
		
		$this->_tables[] = new Find($this, $joinType, $entity, $previusEntity, $customJoin);
		
		return end($this->_tables);
	}

	/**
	 * Metodo che esegue la query costruita in base ai valori impostati nella entity
	 * o a quelli aggiuntivi settati con i metodi presenti in questa classe
	 *
	 * @param boolean $returnArrayAssoc Parametro che imposta se ricevere il risultato sotto forma di array associativo o di array di entity
	 *
	 * @return array
	 */
	public function execute($returnArrayAssoc = false)
	{
		return $this->_functionInstance->execute($returnArrayAssoc);
	}
}
?>
